package com.loukou.lkt.lkt.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.loukou.lkt.lkt.dao.AllocationOrderDetailDao;
import com.loukou.lkt.lkt.dao.GoodsCostDao;
import com.loukou.lkt.lkt.dao.GoodsDao;
import com.loukou.lkt.lkt.dao.PoPurchaseReturnOrderDao;
import com.loukou.lkt.lkt.dao.PoPurchaseReturnOrderDetailDao;
import com.loukou.lkt.lkt.dao.StockOutOrderDao;
import com.loukou.lkt.lkt.dao.StockOutOrderDetailDao;
import com.loukou.lkt.lkt.dao.WarehouseDao;
import com.loukou.lkt.lkt.entity.AllocationOrderDetail;
import com.loukou.lkt.lkt.entity.Goods;
import com.loukou.lkt.lkt.entity.GoodsCost;
import com.loukou.lkt.lkt.entity.PoPurchaseReturnOrderDetail;
import com.loukou.lkt.lkt.entity.StockOutOrder;
import com.loukou.lkt.lkt.entity.StockOutOrderDetail;
import com.loukou.lkt.lkt.entity.Warehouse;
import com.loukou.lkt.lkt.enums.StockOutOrderTypeEnum;
import com.loukou.lkt.lkt.enums.WmsOutOrderTypeEnum;
import com.loukou.lkt.lkt.req.WmsOutOrderCallbackDetailReq;
import com.loukou.lkt.lkt.req.WmsOutOrderCallbackReq;
import com.loukou.lkt.lkt.resp.RespDto;
import com.loukou.lkt.lkt.service.StockOutOrderService;

@Service
public class StockOutOrderServiceImpl implements StockOutOrderService{
	
	@Autowired
	private PoPurchaseReturnOrderDao poReturnDao;
	@Autowired
	private PoPurchaseReturnOrderDetailDao poReturnDetailDao;
	@Autowired
	private StockOutOrderDao stockOutOrderDao;
	@Autowired
	private StockOutOrderDetailDao stockOutOrderDetailDao;
	@Autowired
	private WarehouseDao warehouseDao;
	@Autowired
	private AllocationOrderDetailDao allocationOrderDetailDao;
	@Autowired
	private GoodsCostDao goodsCostDao;
	@Autowired
	private GoodsDao goodsDao;
	
	/**
	 * 新增出库单
	 */
	@Override
	@Transactional
	public RespDto<Integer> addStockOutOrder(WmsOutOrderCallbackReq req) {
		RespDto<Integer> resp = new RespDto<Integer>();
		if(req == null){
			resp.setCode(401);
			resp.setMsg("参数为空");
			return resp;
		}
		
		//获取仓库ID
		Warehouse warehouseMsg=warehouseDao.findByWarehouseCode(req.getWarehouseNo());
		
		int orderType=StockOutOrderTypeEnum.TYPE_RETURN_GOODS.getType();//默认退货
		if(req.getOrderType()==WmsOutOrderTypeEnum.TYPE_RETURN.getType()){//退货
			orderType=StockOutOrderTypeEnum.TYPE_RETURN_GOODS.getType();
		}else if(req.getOrderType()==WmsOutOrderTypeEnum.TYPE_ALLOCATION.getType()){//调拨
			orderType=StockOutOrderTypeEnum.TYPE_AllOCATION.getType();
		}else if(req.getOrderType()==WmsOutOrderTypeEnum.TYPE_CHECK.getType()){//盘点
			orderType=StockOutOrderTypeEnum.TYPE_CHECK.getType();
		}
		
		//添加出库单
		StockOutOrder stockOutOrder=new StockOutOrder();
		stockOutOrder.setOutOrdercode(req.getOutOrderCode());
		stockOutOrder.setOrderType(orderType);
		stockOutOrder.setWarehouseId(warehouseMsg.getWarehouseId());
		stockOutOrder.setDeliveryUserName(req.getDeliveryUserName());
		stockOutOrder.setDeliveryTime(req.getDeliveryTime());
		stockOutOrder.setRemark(req.getRemark());
		StockOutOrder savedStockOutOrder=stockOutOrderDao.save(stockOutOrder);
		int stockOutOrderId=savedStockOutOrder.getId();//获取出库单ID
				
		//添加出库明细
		for(WmsOutOrderCallbackDetailReq outOrderDetail:req.getDetails()){
			StockOutOrderDetail stockOutOrderDetail=new StockOutOrderDetail();
			if(req.getOrderType()==WmsOutOrderTypeEnum.TYPE_RETURN.getType()){//退货
				PoPurchaseReturnOrderDetail ppReturnOrderDetail=poReturnDetailDao.findOne(Integer.parseInt(outOrderDetail.getOutbizId()));
				if(ppReturnOrderDetail == null){
					throw new RuntimeException("退货单明细不存在");
				}
				
				stockOutOrderDetail.setOrderId(stockOutOrderId);
				stockOutOrderDetail.setGoodsId(ppReturnOrderDetail.getGoodsId());
				stockOutOrderDetail.setIsGift(ppReturnOrderDetail.getIsGift());
				stockOutOrderDetail.setTaxPrice(ppReturnOrderDetail.getTaxPrice());
				stockOutOrderDetail.setOrderNumber(ppReturnOrderDetail.getOrderNumber());
				stockOutOrderDetail.setTaxAmount(ppReturnOrderDetail.getTaxAmount());
				stockOutOrderDetail.setDeliveryNumber(outOrderDetail.getDeliveryNumber());
				stockOutOrderDetail.setGoodsName(ppReturnOrderDetail.getGoodsName());
				stockOutOrderDetail.setTaosku(ppReturnOrderDetail.getTaosku());
				stockOutOrderDetail.setBn(ppReturnOrderDetail.getBn());
			}else if(req.getOrderType()==WmsOutOrderTypeEnum.TYPE_ALLOCATION.getType()){//调拨
				AllocationOrderDetail aOrderDetail=allocationOrderDetailDao.findOne(Integer.parseInt(outOrderDetail.getOutbizId()));
				if(aOrderDetail == null){
					throw new RuntimeException("调拨单明细不存在");
				}
				
				stockOutOrderDetail.setOrderId(stockOutOrderId);
				stockOutOrderDetail.setGoodsId(aOrderDetail.getOutGoodsId());
				stockOutOrderDetail.setIsGift(0);
				stockOutOrderDetail.setTaxPrice(0);
				stockOutOrderDetail.setOrderNumber(aOrderDetail.getPlanQuantity());
				stockOutOrderDetail.setTaxAmount(0);
				stockOutOrderDetail.setDeliveryNumber(outOrderDetail.getDeliveryNumber());
				stockOutOrderDetail.setGoodsName(aOrderDetail.getOutGoodsName());
				stockOutOrderDetail.setTaosku(aOrderDetail.getOutGoodsSku());
				stockOutOrderDetail.setBn(aOrderDetail.getOutGoodsBarcode());
			}else if(req.getOrderType()==WmsOutOrderTypeEnum.TYPE_CHECK.getType()){//盘亏出库
				//获取最新成本价
				GoodsCost goodsCostMsg=goodsCostDao.getGoodsCost(outOrderDetail.getGoodsId());
				double taxPrice=goodsCostMsg.getCostPrice();
				double taxAmount=goodsCostMsg.getCostPrice()*outOrderDetail.getDeliveryNumber();
				//获取商品信息
				Goods goodsMsg=goodsDao.findOne(outOrderDetail.getGoodsId());
				
				stockOutOrderDetail.setOrderId(stockOutOrderId);
				stockOutOrderDetail.setGoodsId(goodsMsg.getGoodsId());
				stockOutOrderDetail.setTaxPrice(taxPrice);
				stockOutOrderDetail.setOrderNumber(outOrderDetail.getDeliveryNumber());
				stockOutOrderDetail.setTaxAmount(taxAmount);
				stockOutOrderDetail.setDeliveryNumber(outOrderDetail.getDeliveryNumber());
				stockOutOrderDetail.setGoodsName(goodsMsg.getGoodsName());
				stockOutOrderDetail.setTaosku(goodsMsg.getSku());
				stockOutOrderDetail.setBn(goodsMsg.getBarcode());
			}
			stockOutOrderDetailDao.save(stockOutOrderDetail);
		}

		resp.setMsg("添加成功");
		resp.setResult(stockOutOrderId);
		return resp;
	}
}
